package com.GUI.lesson1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class jiangting2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("开始");
        Button button2 = new Button("停止");

        //可以显示定义触发会返回的命令
        //可以多个按钮只写一个监听类
        button2.setActionCommand("start");

        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}

class MyMonitor implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()获取按钮信息
        //System.out.println("e.getActionCommand()");
        if (e.getActionCommand().equals("start")){
            System.out.println("开始");
        }
        else {
            System.out.println("结束");
        }
    }
}